<script setup>
import { useRouter, useRoute } from "vue-router";
import myChat from "@/views/components/chat";
import useInstructionStore from "@/store/modules/instructionList.js";

const route = useRoute();
const router = useRouter();

const rowItem = ref({});
const chatRef = ref(null);

onMounted(() => {
  // 设置到响应式变量中
  rowItem.value = JSON.parse(route.query.rowItem);
  const answer = {
    ...rowItem.value.answer
  };
  nextTick(() => {
    chatRef.value.refactoringData({
      answer
    });
  });
});

const scrollToBottom = () => {
  nextTick(() => {
    // 等待DOM更新
    const contentDiv = document.querySelector(".chatBox");
    if (contentDiv) {
      contentDiv.scrollTop = contentDiv.scrollHeight; // 设置滚动位置
    }
  });
};
</script>

<template>
  <div class="contentBox">
    <van-nav-bar title="监控警告" left-arrow @click-left="router.go(-1)" />
    <div class="chatContent">
      <div class="title">{{ rowItem.command }}</div>
      <div class="chatBox">
        <myChat
          ref="chatRef"
          :msg="title"
          class="newContent"
          apiType="测试"
          @update:message="
            e => {
              messages = e;
            }
          "
          @scrollToBottom="scrollToBottom"
        />
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.contentBox {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: #f8f8f8;
}
.inputText {
  padding: 10px;
  border-bottom: 1px solid rgba(233, 233, 233);
  .van-cell {
    border: 1px solid #ccc;
    border-radius: 4px;
  }
}
.btnStyle {
  display: flex;
  justify-content: space-around;
  align-items: center;
  width: 80%;
  margin: 20px auto;
  .van-button {
    width: 25%;
  }
}

.chatContent {
  overflow: hidden;
  overflow-y: auto; /* 允许垂直滚动 */
  border: 1px solid rgba(187, 187, 187);
  margin: 0 10px;
  margin-bottom: 20px;
  width: calc(100% - 22px);
  .title {
    background-color: rgba(247, 247, 252);
    width: calc(100% - 16px);
    overflow-wrap: break-word;
    padding: 4px 8px;
    font-size: 14px;
    color: #666666;
    min-height: 20px;
    line-height: 1.5;
    border-bottom: 1px solid rgba(187, 187, 187);
  }
  .chatBox {
    max-height: calc(100vh - 100px);
    overflow-y: auto;
  }
  :deep(.newContent) {
    width: 100%;
    box-sizing: border-box;
    background-color: white;
    .message-container {
      padding: 0 !important;
      margin: 0 !important;
    }
    .bubble {
      border-radius: 0 !important;
      margin: 0 !important;
      padding: 0 !important;
    }
  }
}
</style>
